package com.xiye.system.controller.button;

import com.xiye.common.controller.BaseController;
import com.xiye.common.entity.Page;
import com.xiye.common.entity.PageData;
import com.xiye.common.entity.Result;
import com.xiye.common.util.Const;
import com.xiye.common.util.Jurisdiction;
import com.xiye.common.util.ResultUtil;
import com.xiye.system.config.YmlConfig;
import com.xiye.system.service.button.ButtonService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author 西野
 * @version V1.0
 * @title ButtonController.java
 * @package com.xiye.system.controller.button
 * @description 按钮管理
 * @date 2018-09-20
 */
@RestController
@Api(value = "/button", tags = "按钮管理")
@RequestMapping(value = "/button")
public class ButtonController extends BaseController {

    String menuUrl = Const.MENU_BUTTON; // 菜单地址(权限用)
    @Autowired
    private ButtonService buttonService;
    @Autowired
    private YmlConfig ymlConfig;

    /*
     * @Author 西野
     * @Date 2018-09-20 09:52:30
     * @Description 按钮管理列表
     * @Param [page]
     * @Return com.xiye.common.entity.Result
     */
    @ApiOperation(value = "显示按钮管理列表", notes = "显示按钮管理列表")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "服务器异常")})
    @RequestMapping(value = "/listButton", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public Result list(Page page) throws Exception {
        PageData pd = this.getPageData();
        Map<String, Object> map = new HashMap<String, Object>();
        String keywords = pd.getString("keywords"); // 关键词检索条件
        if (null != keywords && !"".equals(keywords)) {
            pd.put("keywords", keywords.trim());
        }
        page.setPageData(pd);
        List<PageData> varList = buttonService.list(page); // 列出button列表
        map.put("varList", varList);
        map.put("pd", pd);
        map.put("Page", page);
        map.put("QX", Jurisdiction.getHC()); // 按钮权限
        return ResultUtil.setRowbackResut_entity(true, 200, ymlConfig.getServer_success(), map);
    }

    /*
     * @Author 西野
     * @Date 2018-09-20 09:58:17
     * @Description 新增按钮
     * @Param []
     * @Return com.xiye.common.entity.Result
     */
    @ApiOperation(value = "新增按钮", notes = "新增按钮")
    @ApiImplicitParams({@ApiImplicitParam(name = "NAME", value = "按钮名称", required = true), @ApiImplicitParam(name = "QX_NAME", value = "权限名称", required = true), @ApiImplicitParam(name = "BZ", value = "备注", required = true)})
    @RequestMapping(value = "/save", method = RequestMethod.PUT, produces = "application/json;charset=utf-8")
    public Result save() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {
            return ResultUtil.setRowbackResut_entity(false, 403, ymlConfig.getAuthorization(), map);
        }
        PageData pd = this.getPageData();
        pd.put("BUTTON_ID", this.get32UUID()); // 主键
        buttonService.save(pd);
        return ResultUtil.setRowbackResut_entity(true, 200, ymlConfig.getServer_success(), map);
    }

    /*
     * @Author 西野
     * @Date 2018-09-20 10:00:46
     * @Description 删除按钮
     * @Param []
     * @Return com.xiye.common.entity.Result
     */
    @ApiOperation(value = "删除按钮", notes = "根据BUTTON_ID删除按钮")
    @ApiImplicitParam(name = "BUTTON_ID", value = "按钮id", required = true)
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = "application/json;charset=utf-8")
    public Result delete() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (!Jurisdiction.buttonJurisdiction(menuUrl, "del")) {
            return ResultUtil.setRowbackResut_entity(false, 403, ymlConfig.getAuthorization(), map);
        }
        PageData pd = this.getPageData();
        buttonService.delete(pd);
        return ResultUtil.setRowbackResut_entity(true, 200, ymlConfig.getServer_success(), map);
    }

    /*
     * @Author 西野
     * @Date 2018-09-20 10:09:13
     * @Description 修改按钮信息
     * @Param []
     * @Return com.xiye.common.entity.Result
     */
    @ApiOperation(value = "修改按钮信息", notes = "根据BUTTON_ID修改按钮信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "BUTTON_ID", value = "按钮ID", required = true), @ApiImplicitParam(name = "NAME", value = "按钮名称", required = true), @ApiImplicitParam(name = "QX_NAME", value = "权限名称", required = true), @ApiImplicitParam(name = "BZ", value = "备注", required = true)})
    @RequestMapping(value = "/edit", method = RequestMethod.PUT, produces = "application/json;charset=utf-8")
    public Result edit() {
        Map<String, Object> map = new HashMap<String, Object>();
        if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {
            return ResultUtil.setRowbackResut_entity(false, 403, ymlConfig.getAuthorization(), map);
        } // 校验权限
        PageData pd = this.getPageData();
        buttonService.edit(pd);
        return ResultUtil.setRowbackResut_entity(true, 200, ymlConfig.getServer_success(), map);
    }

    /*
     * @Author 西野
     * @Date 2018-09-20 10:16:57
     * @Description 批量删除按钮
     * @Param []
     * @Return java.lang.Object
     */
    @ApiOperation(value = "批量删除", notes = "根据BUTTON_ID数组串批量删除")
    @ApiImplicitParam(name = "DATA_IDS", value = "按钮id", required = true)
    @RequestMapping(value = "/deleteAll", method = RequestMethod.DELETE, produces = "application/json;charset=utf-8")
    public Result deleteAll() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (!Jurisdiction.buttonJurisdiction(menuUrl, "del")) {
            return ResultUtil.setRowbackResut_entity(false, 403, ymlConfig.getAuthorization(), map);
        }
        PageData pd = this.getPageData();
        String DATA_IDS = pd.getString("DATA_IDS");
        if (null != DATA_IDS && !"".equals(DATA_IDS)) {
            String ArrayDATA_IDS[] = DATA_IDS.split(",");
            buttonService.deleteAll(ArrayDATA_IDS);
        } else {
        }
        return ResultUtil.setRowbackResut_entity(true, 200, ymlConfig.getServer_success(), map);
    }


}
